#include "Missile.h"

Missile::Missile( )
{
	m_PosX = 0 ;
	m_PosY = 0 ;

	m_StartX = 0 ;
	m_StartY = 0 ;

	m_TargetX = 0 ;
	m_TargetY = 0 ;
}

void Missile::Init( int startX, int startY, int targetX, int targetY )
{
	m_PosX = startX ;
	m_PosY = startY ;

	m_StartX = startX ;
	m_StartY = startY ;

	m_TargetX = targetX ;
	m_TargetY = targetY ;
}

void Missile::Move( Quadrant* currQuad )
{
	//calculates the difference between x and y sectors (absolute value)
	int diffX, diffY ;

	//finds the difference in x axis
	if( m_PosX > m_TargetX )
		diffX = m_PosX - m_TargetX ;
	else if( m_PosX < m_TargetX )
		diffX = m_TargetX - m_PosX ;
	else
		diffX = 0 ;

	//finds the difference in the y axis (absolute value)
	if( m_PosY > m_TargetY )
		diffY = m_PosY - m_TargetY ;
	else if( m_PosY < m_TargetY )
		diffY = m_TargetY - m_PosY ;
	else
		diffY = 0 ;

	//destroys this missile
	if( diffX == 0 && diffY == 0 )
		delete this ;
	//otherwise, it will move towards the target sector directly based on largest difference
	else
	{
		//move horizontally
		if( diffX >= diffY )
		{
			if( m_PosX > m_TargetX )
				m_PosX-- ;
			else
				m_PosX++ ;
		}
		//move vertically
		else if( diffY > diffX )
		{
			if( m_PosY > m_TargetY )
				m_PosY-- ;
			else
				m_PosY++ ;
		}
	}

	cout << "Missile has moved to sector " << m_PosX << ", " << m_PosY << endl ;
}

int Missile::GetDistance( )
{
	int distanceX = m_StartX - m_TargetX ;
	int distanceY = m_StartY - m_TargetX ;

	if( distanceX < 0 )
		distanceX = -distanceX ;

	if( distanceY < 0 )
		distanceY = -distanceY ;

	if( distanceX >= distanceY )
		return distanceX ;
	else
		return distanceY ;
}